package cst.neu.yuejian_31.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import cst.neu.yuejian_31.Common.Constant;
import cst.neu.yuejian_31.R;
import cst.neu.yuejian_31.bean.ClassCollectBean;
import cst.neu.yuejian_31.bean.ClassStateBean;
import cst.neu.yuejian_31.iface.ClassCollectListener;
import cst.neu.yuejian_31.iface.ClassCollectStateListener;
import cst.neu.yuejian_31.model.ClassCollectModel;
import cst.neu.yuejian_31.model.ClassCollectStateModel;

public class CourseDetailActivity extends BaseActivity implements View.OnClickListener, ClassCollectStateListener,ClassCollectListener {
    private TextView ccName,ccIntro,ccMoney,ccBtime,ccEtime,ccTeacher;
    private ImageView ccPic,btn_retun;
    private String userId,classId,address,money,classname,classpic;
    private Button ccJoin;
    private Context context;
    private ImageView ccCollect;
    private int state,collect;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_detail);
        initViews();
        initDate();
        collectState();
    }

    private void collectState() {
        ClassCollectStateModel model=new ClassCollectStateModel();
        model.getClassState(userId,classId,this);
    }

    private void initDate() {
        userId=getUserId();
        Intent intent=getIntent();
        classId=intent.getStringExtra("course_id");
//        Toast.makeText(CourseDetailActivity.this,classId+userId, Toast.LENGTH_SHORT).show();
        ccName.setText(intent.getStringExtra("course_name"));
        ccIntro.setText(intent.getStringExtra("course_intro"));
        ccBtime.setText(intent.getStringExtra("course_Btime"));
        ccEtime.setText(intent.getStringExtra("course_Etime"));
        ccTeacher.setText(intent.getStringExtra("course_teacher"));
        ccMoney.setText(intent.getStringExtra("course_money"));
        address=intent.getStringExtra("course_address");
        money=intent.getStringExtra("course_money");
        classname=intent.getStringExtra("course_name");
        classpic=intent.getStringExtra("course_pic");
        Picasso.with(context).load(Constant.CLASS_URL+intent.getStringExtra("course_pic").toString()).into(ccPic);

    }

    private void initViews() {
        ccName= (TextView) findViewById(R.id.textView_cclass_name_d);
        ccIntro= (TextView) findViewById(R.id.textView_cclass_intro);
        ccMoney= (TextView) findViewById(R.id.textView_cclass_price);
        ccBtime= (TextView) findViewById(R.id.textView_cclass_Btime);
        ccEtime= (TextView) findViewById(R.id.textView_cclass_Etime);
        ccTeacher= (TextView) findViewById(R.id.textView_cclass_teacher);
        ccPic= (ImageView) findViewById(R.id.imageView_cclass_pic);
        ccCollect= (ImageView) findViewById(R.id.imageView_cclass_collect);
        ccJoin= (Button) findViewById(R.id.button_cclass_join);
        btn_retun= (ImageView) findViewById(R.id.imageView_return_cd);
        ccJoin.setOnClickListener(this);
        ccCollect.setOnClickListener(this);
        btn_retun.setOnClickListener(this);
    }

    @Override
    public void onSuccess(ClassStateBean classStateBean) {
        state=classStateBean.getState();
        if(state==1){
            ccCollect.setImageResource(R.drawable.ic_collect);
        }else {
            ccCollect.setImageResource(R.drawable.ic_uncollect);
        }
    }

    @Override
    public void onSuccess(ClassCollectBean classCollectBean) {
        collect=classCollectBean.getSuccess();
        if (collect==1){
            ccCollect.setImageResource(R.drawable.ic_collect);
            Toast.makeText(CourseDetailActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
        }else {
            ccCollect.setImageResource(R.drawable.ic_uncollect);
            Toast.makeText(CourseDetailActivity.this, "取消收藏", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onFail(String msg) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.imageView_cclass_collect:
                doFavor();
                break;
            case R.id.button_cclass_join:
                Intent intent=new Intent(CourseDetailActivity.this,ClassOrderActivity.class);
                intent.putExtra("course_id",classId);
                intent.putExtra("course_name",classname);
                intent.putExtra("course_money",money);
                intent.putExtra("course_address",address);
                intent.putExtra("course_pic",classpic);
                startActivity(intent);
                break;
            case R.id.imageView_return_cd:
                finish();
                break;
        }
    }

    private void doFavor() {
        ClassCollectModel cmodel=new ClassCollectModel();
        cmodel.classCollect(userId,classId,this);
    }
}
